import os
import threading
import requests
from dotenv import load_dotenv
from lxml import html

from core.utils import mongo

load_dotenv()

headers = {
    'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 SLBrowser/9.0.5.12181 SLBChan/11 SLBVPV/64-bit'
}

lock = threading.Lock()  # 创建线程锁
max_threads = 100  # 最大线程数
semaphore = threading.Semaphore(max_threads)  # 创建信号量对象，限制最大并发线程数为 100

mongo_db = os.getenv("MONGO_DB")
mongo_detail = os.getenv("MONGO_DETAIL")

def get_detail(w_id, url, fw, db):
    try:
        # 获取元素
        response = requests.get(f"{url}param.html", headers=headers)
        response.encoding = 'utf-8'
        html_source = html.fromstring(response.text)

        lis = html_source.xpath('//td[@class="param_info_txt"]//li')
        w_param = list()
        for li in lis:
            w_param.append(li.text_content())

        # 在写入文件时加锁，确保线程安全
        with lock:
            brand, series, movement_type, sex, material, power_reserve = "暂无", "暂无", "暂无", "暂无", "暂无", "暂无"
            for param in w_param:
                if "品牌" in param:
                    brand = param.replace("品牌", "")
                if "系列" in param:
                    series = param.replace("系列", "")
                if "机芯类型" in param:
                    movement_type = param.replace("机芯类型", "").replace(" ", "").replace("\n", "")
                if "性别" in param:
                    sex = param.replace("性别", "").replace(" ", "").replace("\n", "")
                if "表壳材质" in param:
                    material = param.replace("表壳材质", "").replace(" ", "").replace("\n", "")
                if "动力储备" in param:
                    power_reserve = param.replace("动力储备", "").replace(" ", "").replace("\n", "")
            fw.write(f"{w_id},{brand},{series},{movement_type},{sex},{material},{power_reserve}\n")
            mongo.add_one(db, mongo_detail, {'w_id': str(w_id), 'brand': str(brand), 'series': str(series), 'movement_type': str(movement_type), 'sex': str(sex), 'material': str(material), 'power_reserve': str(power_reserve)})
            print(w_id, brand, series, movement_type, sex, material, power_reserve)

    except Exception as e:
        print(f"Error occurred for {url}: {e}")


    finally:
        semaphore.release()  # 释放信号量，允许其他线程执行


if __name__ == '__main__':
    try:
        # 读取数据文件
        db, conn = mongo.get_db(mongo_db)
        with open("../data/list.csv", mode="r", encoding="utf-8") as fr:
            data = fr.readlines()

        # 打开结果文件
        with open("../data/detail.csv", mode="a", encoding="utf-8") as fw:
            threads = []
            for line in data:
                line = line.split(",")
                w_id = line[0]
                url = line[2]

                # 获取信号量，确保最多 100 个线程同时执行
                semaphore.acquire()

                # 创建线程
                thread = threading.Thread(target=get_detail, args=(w_id, url, fw, db))
                threads.append(thread)
                thread.start()

            # 等待所有线程完成
            for thread in threads:
                thread.join()
            conn.close()

    except Exception as e:
        print(f"Error in main execution: {e}")
